import redis
import time

class MonitoredService:
    def __init__(self, dog_name):
        self.redis = redis.Redis(
            host="localhost",
            port=6379,
            decode_responses=True
        )
        self.dog_name = dog_name

    def heartbeat(self):
        """每周期执行的心跳更新"""
        self.redis.set(
            name=self.dog_name,
            value="正常运行",
            ex=60  # TTL 60秒
        )
        print(f"[{self.dog_name}] 心跳已更新")

if __name__ == "__main__":
    service = MonitoredService("service1")
    while True:
        # 模拟业务逻辑运行
        time.sleep(30)
        service.heartbeat()